<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="选择审批人"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :before-close="beforeClose"
    class="tag-edit-dialog tag-user-dialog"
    @closed="clearDialog"
    width="25%"
    v-dialogMove
  >
    <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
      <el-form-item :label="reassign ? '更换审批人' : '下步审批人'" prop="handlerId">
        <el-select v-model="editForm.handlerId" filterable clearable>
          <el-option v-for="item in users" :key="item.userId" :label="$name(item)" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel()">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    procId: {
      // 1、需求;2、注册
      type: Number,
      default: 1
    },
    nodeId: {
      // 节点id
      type: Number,
      required: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      reassign: false,
      users: [],
      editForm: {
        handlerId: ''
      },
      editFormRules: {
        handlerId: [{ required: true, message: '审批人不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    showDialog(reassign = false) {
      this.reassign = reassign
      this.getList()
    },
    getList() {
      let sigh = 10
      if (this.reassign) {
        if (this.nodeId === 35) sigh = 15
        else if (this.nodeId === 40) sigh = 5
      }
      let url = `sys/sysProcNodeRole/userlist/${this.procId}/${this.reassign ? this.nodeId - sigh : this.nodeId}`
      this.$ajax({
        url: this.$ajax.setUrl(url),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.users = data.data
          if (data.data.length) {
            this.editForm.handlerId = data.data[0].userId
            this.dialogVisible = true
          } else {
            this.$message.info('当前未配置审批角色，请配置')
            this.$emit('callback', null)
          }
        } else {
          this.$message.error(data.data || data.msg)
          this.$emit('callback', null)
        }
      })
    },
    submit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          this.$emit('callback', this.editForm.handlerId)
          this.dialogVisible = false
        }
      })
    },
    cancel() {
      this.$emit('callback', null)
      this.beforeClose()
    },
    beforeClose() {
      this.dialogVisible = false
    },
    clearDialog() {
      this.editForm.handlerId = ''
      this.$nextTick(() => this.$refs.editForm.clearValidate())
    }
  }
}
</script>

<style lang="scss">
.tag-user-dialog {
  .el-form {
    margin-top: 20px;
  }
}
</style>
